package com.wxq.workpad.lailiao.dialog;

import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

//import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.QuickViewHolder;
import com.qianfanyun.qfui.rlayout.RTextView;
import com.wxq.workpad.R;
import com.wxq.workpad.entity.SaiXuanItem;
import com.wxq.workpad.lailiao.adapter.SaiXuanListAdapter;
import com.wxq.workpad.utils.DeviceUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能详细描述
 *
 * @author xyx on 2017/8/15
 * @e-mail 384744573@qq.com
 * @see [相关类/方法](可选)
 */

public class ChooseSaiXuanDialog extends Dialog  {

    private Context mContext;
    private String apkInstallPath;
    private String updateUrl = "";
    private ClickItemListener listener;
    SaiXuanListAdapter saiXuanListAdapter;
    public ChooseSaiXuanDialog(Context context) {
        super(context, R.style.DialogTheme);
        mContext = context;
        View mContentView = LayoutInflater.from(mContext).inflate(R.layout.dialog_saixuan, null);
        setContentView(mContentView);
        Window window = getWindow();
        window.setGravity(Gravity.CENTER);  //此处可以设置dialog显示的位置
        window.setLayout(DeviceUtils.dp2px(mContext, 400), ViewGroup.LayoutParams.WRAP_CONTENT); //设置宽高,这样设置才有效
        initView(mContentView);
    }

    RecyclerView recyclerView;
    TextView tv_confirm;
    TextView tv_reset;
//    SaiXuanListAdapter adapter;
    private void initView(View mContentView) {
        tv_confirm=mContentView.findViewById(R.id.tv_confirm);
        tv_reset=mContentView.findViewById(R.id.tv_reset);

        tv_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                listener.getConfirmData(mlist);
                dismiss();

            }
        });
        tv_reset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for (SaiXuanItem saiXuanItem : mlist) {
                    saiXuanItem.setValue("");
                }
                saiXuanListAdapter.notifyDataSetChanged();
            }
        });

        recyclerView=mContentView.findViewById(R.id.recyvleview);
        recyclerView.setLayoutManager(new GridLayoutManager(mContext,3));

        saiXuanListAdapter=new SaiXuanListAdapter(mContext);

        recyclerView.setAdapter(saiXuanListAdapter);
    }


    List<SaiXuanItem> mlist;

    public void showDialog(List<SaiXuanItem> list) {
        this.mlist=list;
        saiXuanListAdapter.updateDate(mlist);
        setCanceledOnTouchOutside(true);
        show();
    }


    public void setListener(ClickItemListener listener) {
        this.listener = listener;
    }

    public interface ClickItemListener {
        /**
         * app内下载Apk文件
         */
        void click(int position);
        void getConfirmData(  List<SaiXuanItem> data);

    }

    public void updateUI(){
        saiXuanListAdapter.notifyDataSetChanged();
    }
}
